using System;

using OpenXmlWriter.Packaging;

namespace OpenXmlWriter.WordprocessingML.Packaging
{
  public class CommentsPart : OpenXmlPart
  {
    private const string PartRelationshipType =
      "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments";

    private const string PartName = "comments";
    private const string PartPath = "word";
    private const string PartExtension = ".xml";
    public const string PartContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml";
    private string _targetPath;

    public override void Initialize(OpenXmlPackage openXmlPackage, OpenXmlPartContainer openXmlPartContainer)
    {
      base.Initialize (openXmlPackage, openXmlPartContainer);
      _targetPath = CreateUniquePath();
      _contentType = new ContentType(_targetPath, PartContentType, false);
    }

    private string CreateUniquePath()
    {
      return PartPath + "/" + PartName + PartExtension;
    }

    public override string RelationshipType
    {
      get
      {
        return PartRelationshipType;
      }
    }

    public override ContentType ContentType
    {
      get
      {
        return _contentType;
      }
    }

    public override void AddTo(OpenXmlPartCollection parts)
    {
      if (parts.ContainsKey(_targetPath))
      {
        throw new InvalidOperationException();
      }

      parts.Add(_targetPath, this);
    }

    public override string TargetPath
    {
      get
      {
        return _targetPath;
      }
    }  
  }
}